package com.webtest.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webtest.config.ExcludeEmptyQueryWrapper;
import com.webtest.dao.AdminMapper;
import com.webtest.entity.Admin;
import com.webtest.entity.User;
import com.webtest.service.admin.AdminUserService;
import com.webtest.vo.SearchStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;
@Controller
@RequestMapping("/admin")
public class AdminAdminController {
    @Autowired
    private AdminMapper adminMapper;

    @RequestMapping("/admins")
    public String listAll(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum, Model model){
        PageHelper.startPage(pageNum,6);
        List<Admin> allUser = adminMapper.selectList(new ExcludeEmptyQueryWrapper<>());
        PageInfo<Admin> userPageInfo = new PageInfo<>(allUser);
        model.addAttribute("pageInfo",userPageInfo);
        return "admin/yg";
    }

    @PostMapping("/admins/search")
    public String search(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                Model model, SearchStudent searchStudent){
        PageHelper.startPage(pageNum,6);
        PageInfo<Admin> userlist =new PageInfo<>(adminMapper.selectList(new ExcludeEmptyQueryWrapper<>()));
        model.addAttribute("pageInfo",userlist);
        return "admin/yg :: stuList";
    }

    @GetMapping("/admins/input")
    public String toAdd(Model model){
        model.addAttribute("students",new Admin());
        return "admin/yg-input";
    }

    @PostMapping("/admins/add")
    public String add(@Valid Admin admin, RedirectAttributes attributes){
        if(adminMapper.selectList(new ExcludeEmptyQueryWrapper<Admin>().eq("username",admin.getUsername())).size()>0){
            attributes.addFlashAttribute("message","账户已添加！！！");
            return "redirect:/admin/admins";
        }
        adminMapper.insert(admin);
        attributes.addFlashAttribute("message","添加成功！！！");
        return "redirect:/admin/admins";
    }

    @GetMapping("/admins/{id}/update")
    public String toUpdate(@PathVariable Integer id, Model model){
        System.out.println("toUpdateStu的id：" + id);
        Admin userById = adminMapper.selectById(id);
        System.out.println("userById： "+ userById);
        model.addAttribute("students",userById);
        return "admin/yg-input";
    }

    @PostMapping("/admins/update")
    public String update(Admin admin,RedirectAttributes attributes){
        adminMapper.updateById(admin);
        attributes.addFlashAttribute("message", "修改成功！！！");
        return "redirect:/admin/admins";
    }

    @GetMapping("/admins/{id}/delete")
    public String delete(@PathVariable Integer id,RedirectAttributes attributes){
        adminMapper.deleteById(id);
        attributes.addFlashAttribute("message","删除用户成功");
        return "redirect:/admin/admins";
    }
}
